<template>
	<view class="billdetails">
		<view class="billdetails_top">
			<view class="name">{{data.name}}{{data.address}}</view>
			<view class="moeny">{{Number(data.money).toFixed(2)}}</view>
			<view class="last">{{billList.order.status==1?'待缴费':'已缴费'}}</view>
		</view>
		<!-- 分割线 -->
		<view class="xian ">
			<view class="xian_left"></view>
			<view class="xian_center"></view>
			<view class="xian_right"></view>
		</view>
		<!-- 主体内容 -->
		<view class="main">
			<view class="main_jh">
				<view class="zdbh clearfix">
					<view class="bh">账单编号</view>
					<view class="num">{{data.code}}</view>
				</view>
			</view>
			<view class="main_jh">
				<view class="zdbh clearfix">
					<view class="bh">缴费时间</view>
					<view class="num">{{billList.order.createTime}}</view>
				</view>
			</view>
			<view class="main_jh">
				<view class="zdbh clearfix">
					<view class="bh">缴费名称</view>
					<view class="num">{{billList.order.name}}</view>
				</view>
			</view>
			<view class="u-p-r-40 u-p-l-40 bill-deta-btn" v-show="billList.order.status==1">
				<button @click="payClick" class="yilingqu">缴费</button>
			</view>
		</view>
		<button class="btn typography" open-type="launchApp" v-if="statu" app-parameter="wechat"
			binderror="launchAppError">返回APP</button>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				allmoney: '3600.00',
				type: '已支付',
				num: 'WY20200807',
				billList: {},
				homelist: '',
				chooseList: [],
				data: {
					id: '',
					mobile: '',
					proId: '',
					code: '',
					name: '',
					address: '',
					money: '',
					setId: '',
					childList: []
				},
				orderObj:{},
				statu: false
			}
		},
		async onLoad(option) {	
			let storageObj = uni.getStorageSync('orderDetailList')
			console.log(storageObj == "",storageObj==null, storageObj,123);
			if (storageObj) {
				console.log('从app来源')
				let dataObj = JSON.parse(storageObj);
				this.statu = true
				//let data = dataObj;
				this.billList = dataObj.billList;
				this.data = dataObj //JSON.parse(dataObj);
				// return
			} else {
				// -- console.log('来源于wx小程序本身')
				this.statu = false
				this.homelist = uni.getStorageSync('hourseList')[uni.getStorageSync('hourseNum') || 0]
				this.data.mobile = uni.getStorageSync('phone');
				this.data.name = this.homelist.proName;
				this.data.address = this.homelist.buildDistrictCode + '-' + this.homelist.buildTowerCode + '-' + this
					.homelist.roomUnitNumber +
					'-' + this.homelist.roomCode
				this.data.money = this.$Route.query.money
				this.chooseList = this.$Route.query.chooseList
				this.data.id = this.$Route.query.id;
				this.data.proId = this.homelist.proId;
				this.data.setId = this.homelist.setId;
			}
			await this.getBillDetail()
		},
		methods: {
			async getBillDetail() {
				let res = await this.$request.api.billDetails({
					setId: this.data.setId,
					id: this.data.id,
					mobile: this.data.mobile
				})
				console.log(res,this.data.id,this.data.setId,this.data.mobile, 'asd')
				this.billList = res.data.data.data
				console.log(this.billList,'--------获取订单详情1-------',this.billList.order.code);
				this.data.code = this.billList.order.code
					this.data.childList = this.billList.childList;
				//}
			},
			async payClick() {
				let that = this;
				let openId = uni.getStorageSync('openId');
				console.log(openId)
				if(!openId){
					uni.showToast({
						icon: 'none',
						title: '请先登录'
					})

					return
				}
				uni.login({
					provider: 'weixin',
					success: loginRes => {
						uni.setStorageSync('code', loginRes.code);
						that.$request.api.login({
							code: loginRes.code
						}).then(res => {
							 console.log(res,88888)
							uni.setStorageSync('openId', res.data.data.data.openId);
							uni.setStorageSync('sessionKey', res.data.data.data.sessionKey);
						});
					}
				})
				// console.log(uni.getStorageSync('openId'),33333)
				let res = await this.$request.api.getWxPay({
					id: this.data.id,
					mobile: this.data.mobile,
					open_id: uni.getStorageSync('openId'),
					proId: this.data.proId,
					pay_type: 'wx_lite', //（支付渠道）
					goods_title: '小程序物业收费', //（商品标题）
					goods_desc: '小程序物业收费', //（商品描述信息）
					allMoney: Number(this.data.money).toFixed(2),
					code: this.billList.order.code
				})
				// console.log(res,66666)
				if (res.data.status != 200) {
					uni.showToast({
						icon: 'none',
						title: res.data.data.msg
					})
					return
				}else{
					uni.showToast({
						icon: 'error',
						title: res.data.data.msg
					})
				}
				let pay_info = JSON.parse(res.data.data.data.expend.pay_info)

				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: pay_info.timeStamp,
					nonceStr: pay_info.nonceStr,
					package: pay_info.package,
					signType: pay_info.signType,
					paySign: pay_info.paySign,
					success: function(res) {
						console.log('success:' + JSON.stringify(res));
						that.getBillDetail();
					},
					fail: function(err) {
						console.log('fail:' + JSON.stringify(err));
						uni.showToast({
							icon: 'none',
							title: '取消支付'
						})
					}
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	page {
		background-color: rgb(244, 244, 244);

	}

	.billdetails {
		padding: 0 30rpx;
	}

	.billdetails_top {
		height: 312rpx;
		text-align: center;
		background-color: #fff;
		margin-top: 60rpx;
		border-top-left-radius: 6px;
		border-top-right-radius: 6px;

		.name {
			padding-top: 60rpx;
			box-sizing: border-box;

		}

		.last {
			margin-top: 20rpx;
			color: rgb(128, 229, 255);
		}

		.moeny {
			font-size: 48rpx;
			color: #000;
			font-weight: bold;
			margin-top: 60rpx;
		}
	}

	.xian {
		height: 50rpx;
		background-color: #fff;
		display: flex;
		justify-content: space-between;

		.xian_center {
			height: 1px;
			border-top: 1px dashed #ccc;
			margin: 25rpx 40rpx;
			width: 600rpx;

		}

		.xian_left {
			width: 50rpx;
			height: 50rpx;
			border-radius: 50%;
			float: left;
			background-color: rgb(244, 244, 244);
			margin-left: -7vw;
		}

		.xian_right {
			width: 50rpx;
			height: 50rpx;
			border-radius: 50%;
			float: right;
			background-color: rgb(244, 244, 244);
			margin-right: -7vw;

		}
	}

	.zdbh {
		margin-bottom: 30rpx;

		.bh {
			float: left;
		}

		.num {
			float: right;
		}
	}

	.wyf {
		.wyfname {
			float: left;
		}

		.wyfmoney {
			float: right;
		}

		.view {
			margin-bottom: 30rpx;
		}
	}

	.main_jh {
		margin-bottom: 60rpx;
	}

	.main {
		background-color: #fff;
		padding: 60rpx 20rpx;
	}

	.bill-deta-btn {
		width: 100%;
		height: 40rpx;
	}
	.yilingqu{
		width: 80%;
		height: 84rpx;
		background: linear-gradient(180deg, #76D904 0%,#417505 100%);
		border-radius: 44rpx;
		line-height: 84rpx;
		color: #FFFFFF;
		text-align: center;
		font-size: 32rpx;
	}
</style>
